-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2014 Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
printf = require "mod_print"
iHEX = require "mod_iHEX"

hex_file = arg[1]

if hex_file == nil then
	print("Error: Need HEX file name")
	return
end

local pgm_start_addr
local pgm_raw_data
pgm_start_addr, pgm_raw_data = iHEX.Read_File(hex_file)

if type(pgm_raw_data) == "table" then
	print(string.format("\r\nFW start address = 0x%04x, size = %d", pgm_start_addr, #pgm_raw_data))
	--printf.table(pgm_raw_data, true)
else
	print("Error: fail to read hex file.")
	return
end

local file_name = hex_file .. ".bin"
local file_w = io.open(file_name, "wb")
for i = 1, #pgm_raw_data do
	file_w:write(string.char(pgm_raw_data[i]))
end
file_w:close()